﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string tableName = Request.QueryString["tableName"];
            if (tableName == null)
            {
                Response.SuppressContent = true;
                Response.StatusCode = 400;
                return;
            }
            CreateTable(tableName);
        }
    }

    protected void CreateTable(string table)
    {
        Table1.Rows.Clear();
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        Moddb08_WCF.Rows rows = frontEnd.GetAllRows(table);
        DataTable dataTable = frontEnd.GetDataTable(table);
        TableHeaderRow headerRow = new TableHeaderRow();
        headerRow.EnableViewState = true;
        TableHeaderCell checkBoxHeader = new TableHeaderCell();
        checkBoxHeader.Text = "";
        headerRow.Cells.Add(checkBoxHeader);
        Table1.Rows.Add(headerRow);
        Moddb08_WCF.Columns columns = frontEnd.GetAllColumns(table);
        for (int c = 0; c < columns.Count; c++)
        {
            TableHeaderCell hCell = new TableHeaderCell();
            Literal lit = new Literal();
            lit.Text = "<b>" + columns[c].Name + "</b> <br/> <i>" + columns[c].Mime + "</i>";
            hCell.Controls.Add(lit);
            headerRow.Cells.Add(hCell);
        }
        for (int i = 0; i < rows.Count; i++)
        {
            TableRow tableRow = new TableRow();
            tableRow.EnableViewState = true;

            TableCell checkbox = new TableCell();
            checkbox.EnableViewState = true;
            CheckBox cBox = new CheckBox();
            cBox.EnableViewState = true;
            cBox.ID = i.ToString();
            checkbox.Controls.Add(cBox);
            tableRow.Cells.Add(checkbox);

            for (int j = 0; j < columns.Count; j++)
            {
                if (columns[j].Mime.Equals("text/plain") && columns[j].Name.Equals("id"))
                {
                    TableCell cell = new TableCell();
                    cell.Text = dataTable.Rows[i][j].ToString();
                    tableRow.Cells.Add(cell);

                }
                else
                {
                    TableCell cell = new TableCell();
                    Literal lit = new Literal();
                    lit.Text = "<a href=\"" + dataTable.Rows[i][j].ToString() + "\">Link</a>";
                    cell.Controls.Add(lit);
                    tableRow.Cells.Add(cell);
                }
            }
            Table1.Rows.Add(tableRow);
        }
    }

    

    private void CreateEditTable(List<int> editIndexes, string tableName)
    {
        Table1.Rows.Clear();
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        Moddb08_WCF.Columns columns = frontEnd.GetAllColumns(tableName);
        DataTable dataTable = frontEnd.GetDataTable(tableName);
        for (int i = 0; i < editIndexes.Count; i++)
        {
            TableRow row = new TableRow();
            row.EnableViewState = true;
            for (int j = 0; j < columns.Count; j++)
            {
                if (columns[j].Mime.Equals("text/plain"))
                {
                    TableCell tCell = new TableCell();
                    tCell.EnableViewState = true;
                    TextBox tBox = new TextBox();
                    tBox.EnableViewState = true;
                    tBox.ID = "TextBox_" + i.ToString() + "_" + j.ToString();
                    tBox.Text = dataTable.Rows[editIndexes[i]][j].ToString();
                    tCell.Controls.Add(tBox);
                    row.Cells.Add(tCell);
                }
                else
                {
                    TableCell tCell = new TableCell();
                    tCell.EnableViewState = true;
                    FileUpload fUpload = new FileUpload();
                    fUpload.ID = "FileUpload_" + i.ToString() + "_" + j.ToString();
                    fUpload.EnableViewState = true;
                    tCell.Controls.Add(fUpload);
                    row.Cells.Add(tCell);
                }
            }

            Table1.Rows.Add(row);
            Table1.EnableViewState = true;

        }
    }
    #region old code
    //___________________________________________________________________________


    /*private void CreateColumnsInGridView(string tabelName)
    {
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        DataTable table = frontEnd.GetDataTable(tabelName);
        Moddb08_WCF.Columns columns = frontEnd.GetAllColumns(tabelName);
        GridView1.Columns.Clear();

        foreach (DataColumn col in table.Columns)
        {
            Moddb08_WCF.Column column = columns.Find(c => c.Name == col.ColumnName);
            if (!column.Mime.Equals("text/plain"))
            {

                TemplateField tempField = new TemplateField();
                tempField.ItemTemplate = new LinkTemplate(DataControlRowType.DataRow, col.ColumnName);
                tempField.HeaderText = col.ColumnName;
                tempField.EditItemTemplate = new EditLinkTemplate(col.ColumnName);
                GridView1.Columns.Add(tempField);

                /*HyperLinkField hyperLinkField = new HyperLinkField();
                hyperLinkField.HeaderText = col.ColumnName;
                string[] stringCollection = new string[1];
                stringCollection[0] = col.ColumnName;
                hyperLinkField.DataNavigateUrlFields = stringCollection;
                hyperLinkField.DataNavigateUrlFormatString = "{0}"; 
                //hyperLinkField.DataTextField = col.ColumnName;
                //hyperLinkField.DataTextFormatString = "{0}";
                hyperLinkField.Text = "Link";
                GridView1.Columns.Add(hyperLinkField);*/
            /*}
            else
            {
                BoundField field = new BoundField();
                field.HeaderText = col.ColumnName;
                field.DataField = col.ColumnName;
                GridView1.Columns.Add(field);
            }
        }
        GridView1.AutoGenerateEditButton = true;
        GridView1.AutoGenerateDeleteButton = true;
        GridView1.DataSource = table;

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        string test = TablesDropDownList.SelectedValue;
        CreateColumnsInGridView(test);
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataBind();
        Session["SelectedRowIndex"] = e.NewEditIndex;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        DataTable table = frontEnd.GetDataTable(TablesDropDownList.SelectedValue);
        for (int i = 0; i < table.Columns.Count; i++)
        {
            DataControlFieldCell cell = GridView1.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
            GridView1.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
            Control c = FindControlRecursive(GridView1.Rows[e.RowIndex], table.Columns[i].ColumnName + "_file");


        }
        int id = e.NewValues.Count;
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        CreateColumnsInGridView(TablesDropDownList.SelectedValue);
        GridView1.EditIndex = -1;
        GridView1.DataBind();
    }*/

    //_____________________________________________________________________________
    #endregion
    protected void Create_Click(object sender, EventArgs e)
    {
        string tableName = Request.QueryString["tableName"];
        if (tableName == null)
        {
            Response.StatusCode = 400;
            Response.SuppressContent = true;
            return;
        }
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        try
        {
            frontEnd.CreateRowInTable(tableName);
        }
        catch (System.ServiceModel.EndpointNotFoundException)
        {
            Label1.Text = "No table found!";
        }
        CreateTable(tableName);
        
    }
    protected void Delete_Click(object sender, EventArgs e)
    {
        string tableName = Request.QueryString["tableName"];
        int numberOfRowsDeleted = 0;
        if (tableName == null)
        {
            Response.StatusCode = 400;
            Response.SuppressContent = true;
            return;
        }
        try
        {
            ModdbFrontEnd frontEnd = new ModdbFrontEnd();
            Moddb08_WCF.Rows rows = frontEnd.GetAllRows(tableName);
            for (int i = 0; i < rows.Count; i++)
            {
                Object o = Request.Form[i.ToString()];
                if (o != null)
                {
                    string checkBox = o.ToString();
                    if (checkBox.Equals("on"))
                    {
                        frontEnd.DeleteRowInTable(tableName, rows[i].Id.ToString());
                        numberOfRowsDeleted++;
                    }
                }
            }
            Label1.Text = "Number of rows deletes: " + numberOfRowsDeleted.ToString();
            
        }
        catch (System.ServiceModel.EndpointNotFoundException)
        {
            Label1.Text = "No table found!";
        }
        CreateTable(tableName);
    }
    protected void CreateColumn_Click(object sender, EventArgs e)
    {

        string tableName = Request.QueryString["tableName"];
        if (tableName == null)
        {
            Response.StatusCode = 400;
            Response.SuppressContent = true;
            return;
        }
        if (ColumnName.Text.Equals("") || ColumnName.Text == null)
        {
            Label1.Text = "You must enter a table name";
            return;
        }

        if (ColumnMimeType.Text.Equals("") || ColumnMimeType == null)
        {
            Label1.Text = "You must enter a mime type";
            return;
        }

        if (Types.SelectedValue.Equals(""))
        {
            Label1.Text = "You must select a type of the column";
            return;
        }

        Moddb08_WCF.Column col = new Moddb08_WCF.Column();
        col.Mime = ColumnMimeType.Text;
        col.Name = ColumnName.Text;
        col.Type = Types.SelectedValue;
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        frontEnd.AddColumnToTable(tableName, col);
        CreateTable(tableName);

    }
}
